# -*- coding: utf-8 -*-
# Author: xiantingDeng
# File: 08_网络爬虫案例实战01.py
# Time: 10:34
'''
from urllib import request,parse
import json

url = "http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=202125101126"

data = {
    "email":"18574866512",
    "icode":"",
    "origURL":"http://www.renren.com/home",
    "domain":"renren.com",
    "key_id": 1,
    "captcha_type":"web_login",
    "password":"918cb7ba648e1d3f1aeee8679127318f6f3ce71bd90c19d696b402f8e445bac1",
    "rkey":"e85e89d5b673c4d6ba6c114a47c6a7f1",
    "f":"http%3A%2F%2Fwww.renren.com%2F976416062%2Fnewsfeed%2Fphoto"
}

data = parse.urlencode(data)

headers  = {
    "Content-Length": len(data)
}

req = request.Request(url=url,data=bytes(data,encoding="utf-8"),headers=headers)
res = request.urlopen(req).read().decode("utf-8")
print(res)'''

'''
from urllib import request
import re,gzip

base_url = 'http://www.renren.com/965541786'
headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'max-age=0',
    'Connection': 'keep-alive',
    'Cookie': 'anonymid=jgdcqcjgqy4yxt; depovince=BJ; _r01_=1; JSESSIONID=abc7HUv9M_HsB7WkgK2lw; ick_login=b954cf62-bbe5-480d-b679-e1e3ce584896; SL_GWPT_Show_Hide_tmp=1; SL_wptGlobTipTmp=1; ick=b4337770-b7ce-4a70-b9d0-cd63c7fc7bb5; XNESSESSIONID=738f4bde312f; jebe_key=f950add1-40e8-4009-a157-bfc3d89f7350%7C24a48cb369f8637c5ee2c4a23eb5b93f%7C1524555370510%7C1%7C1524555375485; first_login_flag=1; ln_uact=13520319616; ln_hurl=http://head.xiaonei.com/photos/0/0/men_main.gif; wp_fold=0; jebecookies=ef7f7372-0e70-45db-aaae-c415d4611918|||||; _de=8C2F648D7158ED727318288C8F3F21C5; p=f1ea4b6984cefb7d88164a67816c91fe6; t=401516286d37bde6735180d25f68f2fe6; societyguester=401516286d37bde6735180d25f68f2fe6; id=965541786; xnsid=928c27b; ver=7.0; loginfrom=null',
    'Host': 'www.renren.com',
    'Referer': 'http://www.renren.com/SysHome.do',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
}

req = request.Request(url=base_url,headers=headers)
res = request.urlopen(req)

# print(res.read().decode("utf-8"))
#html = res.read().decode("utf-8") # 网页响应时开启了gzip压缩，需要解压
#报错：UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte
# 获取将请求头中的Accept-Encoding的gzip删除也可以

# 对gzip压缩的响应，我们解压后转码
html = gzip.decompress(res.read()).decode("utf-8")
print(html)
# print(re.findall("<title>(.*?)</title>",html))
'''

from urllib import request,parse
import re,gzip,time

# cookie管理模块
from http import cookiejar
# 返回存储cookie对象
cookie = cookiejar.CookieJar()
# 返回一个cookie管理器
cookie_handler = request.HTTPCookieProcessor(cookie)

# 请求管理器
opener = request.build_opener(cookie_handler)

def deLogin():
    login_url = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=2018321648829'
    data = {
        'email': '1352*****16',
        'icode': '',
        'origURL': 'http://www.renren.com/home',
        'domain': 'renren.com',
        'key_id': '1',
        'captcha_type': 'web_login',
        'password': '478b7c2dca554eeabed3b7374703bff4a6a22e78b8a9fcfb090e3a7fb792992b',
        'rkey': 'e954ec64a7ecf4e33bdf81bb1abad158',
        'f': 'http%3A%2F%2Fwww.renren.com%2F965541786',
    }
    data = parse.urlencode(data)

    headers = {
        'Content-Length': len(data)
    }
    req = request.Request(url=login_url, data=bytes(data, encoding='utf-8'), headers=headers, )
    response = opener.open(req)

def myHome():
    home_url = "http://www.renren.com/965541786"
    res = opener.open(home_url)
    html = res.read().decode("utf-8")
    print(re.findall("<title>(.*?)</title>",html))


if __name__ == '__main__':
    print("正在登陆中...")
    deLogin()
    time.sleep(3)
    myHome()